package com.ipzoe.oms.domain;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.experimental.Accessors;
import com.ipzoe.common.annotation.Excel;

import java.io.Serializable;
import java.util.Date;
import java.math.BigDecimal;
import com.ipzoe.common.core.domain.BaseEntity;

/**
 * 订单对象 oms_order
 * 
 * @author eric
 * @date 2020-09-06
 */
@Data
@ToString
@EqualsAndHashCode
@NoArgsConstructor
@Accessors(chain = true)
@TableName("oms_order")
public class Order implements Serializable {

private static final long serialVersionUID=1L;


    /** $column.columnComment */
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    /** 订单号码 */
    @Excel(name = "订单号码")
    private String orderSn;

    /** 购买用户 */
    @Excel(name = "购买用户")
    private Long memberId;

    /** 会员卡券id */
    @Excel(name = "会员卡券id")
    private Long cardId;

    /** 会员卡号 */
    @Excel(name = "会员卡号")
    private String cardNum;

    /** 经销商id */
    @Excel(name = "经销商id")
    private Long dealerId;

    /** 充值套餐id */
    @Excel(name = "充值套餐id")
    private Long packageId;

    /** 提交时间 */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @TableField(fill = FieldFill.INSERT)
    private Date createTime;

    /** 订单总金额 */
    @Excel(name = "订单总金额")
    private BigDecimal totalAmount;

    /** 应付金额（实际支付金额） */
    @Excel(name = "应付金额" , readConverterExp = "实=际支付金额")
    private BigDecimal payAmount;

    /** 支付方式 */
    @Excel(name = "支付方式")
    private Integer payType;

    /** 订单来源 */
    @Excel(name = "订单来源")
    private Integer sourceType;

    /** 订单状态 */
    @Excel(name = "订单状态")
    private Integer status;

    /** 订单备注 */
    @Excel(name = "订单备注")
    private String note;

    /** $column.columnComment */
    private Integer delFlag;

    /** 支付时间 */
    @Excel(name = "支付时间" , width = 30, dateFormat = "yyyy-MM-dd")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date paymentTime;

    /** 订单类型 */
    @Excel(name = "订单类型")
    private Integer orderType;
    @Excel(name = "会员到期时间")
    private Date memberEndTime;
    @Excel(name = "支付状态")
    private Integer payStatus;

    private String payImgName;

    private String payResult;

    private String packageSnapshot;

    public static String toPayParam(String orderSn, String payAmount, Long packageId, Long memberId, Integer payType, String remark){
        return orderSn+"|"+payAmount+"|"+packageId+"|"+memberId+"|"+payType+"|"+remark;
    }

    public static Order parseFromPayParam(String payParam){
        if(StrUtil.isBlank(payParam)){
            return null;
        }
        String[] paramArr = StrUtil.split(payParam, "|");
        if(6!= paramArr.length){
            return null;
        }
        Order order = new Order();
        order.setOrderSn(paramArr[0])
                .setTotalAmount(BigDecimal.valueOf(Long.valueOf(paramArr[1])))
                .setPayAmount(BigDecimal.valueOf(Long.valueOf(paramArr[1])))
                .setPackageId(Long.valueOf(paramArr[2]))
                .setMemberId(Long.valueOf(paramArr[3]))
                .setPayType(Integer.valueOf(paramArr[4]))
                .setNote(paramArr[5]);
        return order;
    }

    public static void main(String[] args) {
        String str = "{\"gmt_create\":\"2020-10-20 00:31:54\",\"charset\":\"UTF-8\",\"seller_email\":\"jojoguo@danchina.com\",\"subject\":\"dan潜水-会员充值\",\"sign\":\"F53kwCm/17BEkc1hvL+ge/RMT9cP4J7tOZmAbz4JeLOZR9lQguGP6mOvO+M53UxmI3ktp5FKsbp58GFOvjORGf1KNouhpvCtFZuOnyEHfexyqx94Tk17w8KS/j9XFtgF2TB4cU2Te1AnMyijZurySiA9KMHyiI+FrMXVG16lAuD79jFqwfLPnBsOTP02EbzOelb0DgbDQBJt0XX8mfzmZ+hp/WTYSKeOIgWJNn7sK7PMXIM8FFbZ/Gj3lX/0zlJGVLZ6I7q5kxkRCD+moVrNyrViqY3Axfoc7VLEqlSaN+vKN2omOtCoQg4N17uY/tnIRZpfF/B8408RqNSqsz/M4g==\",\"body\":\"O202010200031504740|1|1|11124|2|ali_pc\",\"buyer_id\":\"2088202998155964\",\"invoice_amount\":\"0.01\",\"notify_id\":\"2020102000222003158055965702412988\",\"fund_bill_list\":\"[{\\\"amount\\\":\\\"0.01\\\",\\\"fundChannel\\\":\\\"PCREDIT\\\"}]\",\"notify_type\":\"trade_status_sync\",\"trade_status\":\"TRADE_SUCCESS\",\"receipt_amount\":\"0.01\",\"app_id\":\"2021001193600710\",\"buyer_pay_amount\":\"0.01\",\"sign_type\":\"RSA2\",\"seller_id\":\"2088831892298633\",\"gmt_payment\":\"2020-10-20 00:31:57\",\"notify_time\":\"2020-10-20 00:31:58\",\"version\":\"1.0\",\"out_trade_no\":\"O202010200031504740\",\"total_amount\":\"0.01\",\"trade_no\":\"2020102022001455965700473531\",\"auth_app_id\":\"2021001193600710\",\"buyer_logon_id\":\"eri***@gmail.com\",\"point_amount\":\"0.00\"}";
       JSONObject jsonObject = JSON.parseObject(str);
        Order order = Order.parseFromPayParam(jsonObject.getString("body"));
        System.out.println(JSON.toJSONString(order));
    }
}
